﻿namespace MsForgeReboot.Web.Areas.ControlPanel.Controllers {
    #region Usings

    using System;
    using System.Linq;
    using System.Web.Mvc;
    using MsForgeReboot.Framework.Entities;
    using MsForgeReboot.Framework.Interfaces;
    using MsForgeReboot.Framework.ViewModels;
    using MsForgeReboot.Services;

    #endregion

    public class CmsController : Controller {
        private readonly ICmsService _cmsService;

        public CmsController(ICmsService svc) {
            _cmsService = svc;
        }

        #region Public Methods and Operators

        public ActionResult Index() {
            return RedirectToAction("Sections");
        }
        [HttpGet]
        public ActionResult Sections() {
            //var ctx = new MsForgeContext();
            //var results = ctx.Sections.Take(10);
            var results = _cmsService.GetSections();
            return View(results);
        }

        [HttpGet]
        public ActionResult Create() {
            return this.View("Create");
        }

        [HttpPost]
        public ActionResult Create(Section model) {
            if (ModelState.IsValid) {
                _cmsService.Save(model);
                return this.RedirectToAction("Sections");
            }
            return this.View("Create");
        }

        //[HttpGet]
        //public ActionResult Edit(int id) {
        //    var section = _cmsService.GetSingleSection(id);

        //    return this.View("Edit", section);

        //}

        [HttpPost]
        public ActionResult Edit(Section model) {
            if (ModelState.IsValid) {
                _cmsService.Save(model);
                //if (Request.IsAjaxRequest()) return Json(_cmsService.GetSections());
                if (Request.IsAjaxRequest()) return Json(_cmsService.GetSingleSection((model.Id)));
                return RedirectToAction("Sections");
            }
            return this.View("Edit", model);
        }

        [HttpGet]
        public ActionResult Delete(int id) {
            _cmsService.Delete(id);
            return RedirectToAction("Sections");
        }

        [HttpGet]
        public ActionResult Page(int id) {
            // Get section for the ID
            Section section = _cmsService.GetSingleSection(id);
            PageAddViewModel model = new PageAddViewModel();
            if (section.Page != null) {
                model.Title = section.Page.Title;
                model.Body = section.Page.Body;
                model.Id = section.Page.Id;
            } else {
                model.Id = 0;
            }
            model.SectionID = section.Id;
            // Get only the last part of the UrlPointer
            model.UrlPointer = section.UrlPointer.Substring(section.UrlPointer.LastIndexOf("/") + 1);
            
            //string url = section.UrlPointer;
            //ViewBag.UrlPointer = url.Substring(url.LastIndexOf("/") + 1);
            return PartialView("AddPage", model);
        }

        // TODO make a decision do we need this now?
        [HttpGet]
        public ActionResult Pages(int id) {
            var page = _cmsService.GetPageForSection(id);
            return this.View("Edit", page);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Pages(PageAddViewModel item) {
            item.Published = DateTime.Now;
            if (ModelState.IsValid)
                _cmsService.SavePage(item);
            return this.RedirectToAction("Sections");
        }




        #endregion
    }
}